import fs from "fs";
import fp from "fastify-plugin";
import dotenv from "dotenv";

const localEnv = ".env.local";

function envPlugin(app, opt, next) {
  const result = dotenv.config().parsed;
  if (fs.existsSync(localEnv)) {
    const localResult = dotenv.config({
      path: localEnv,
    });
    Object.assign(result, localResult.parsed);
  }
  app.decorate("getEnvs", () => {
    return result;
  });
  app.decorateRequest("getEnvs", () => {
    return result;
  });

  next();
}
export default fp(envPlugin);
